import { defineConfig, loadEnv } from "vite";
import generateSitemap from "vite-ssg-sitemap";


import { getRootPath, getSrcPath, setupVitePlugins } from "./build";

export default defineConfig(configEnv =>{

  const rootPath = getRootPath();
  const srcPath = getSrcPath();

  const viteEnv = loadEnv(
    configEnv.mode,
    process.cwd()
  ) as unknown as ImportMetaEnv;

  return {
    resolve: {
      alias: {
        "~/": `${rootPath}/`,
        "@/": `${srcPath}/`,
      },
    },

    plugins: setupVitePlugins(viteEnv),

    // https://github.com/vitest-dev/vitest
    test: {
      include: ["test/**/*.test.ts"],
      environment: "jsdom",
      deps: {
        inline: ["@vue", "@vueuse", "vue-demi"],
      },
    },

    // https://github.com/antfu/vite-ssg
    ssgOptions: {
      script: "async",
      formatting: "minify",
      onFinished() {
        generateSitemap();
      },
    },

    ssr: {
      // TODO: workaround until they support native ESM
      noExternal: ["workbox-window", /vue-i18n/],
    },
  };
});
